package com.u.social.recommend.service.articles;

import com.u.social.recommend.model.ArticleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: u-social
 * @description: 精排
 * @author: Alex Wu
 * @createDate: 2025-04-08 10:38
 **/

@Service
public class FineRankingService {

    @Autowired
    private AIApiService aiApiService;


    public List<ArticleDTO> elaborateSort(List<Long> articleIds, Long userId) {
        // Step1: 从Redis上去取文章数据
        List<ArticleDTO> articleDTOs = fetchArticleDTOs(articleIds);
        if (articleDTOs == null || articleDTOs.isEmpty()) {
            System.out.println("没有文章数据");
            return null;
        }
        // Step2: 从Redis上去取用户特征数据
        List<String> userFeatures = fetchUserFeatures(userId);
        if (userFeatures == null || userFeatures.isEmpty()) {
            System.out.println("没有用户特征数据");
            return null;
        }
        // Step3: 调用AI能力构建总特征
        List<String> features = aiApiService.generalRankFeatures(articleDTOs, userFeatures);

        // Step 4: 模型预测打分
        List<Float> rankScores = aiApiService.predictRanking(features);


        // Step 5: 排序
        for (int i = 0; i < articleDTOs.size(); i++) {
            articleDTOs.get(i).setScore(rankScores.get(i));
        }

        // Step 6: 根据分数进行排序
        articleDTOs.sort((a1, a2) -> Float.compare(a2.getScore(), a1.getScore()));


        // Step 7: 返回排序后的文章列表前50个
        List<ArticleDTO> sortedArticles = new ArrayList<>();
        for (int i = 0; i < Math.min(50, articleDTOs.size()); i++) {
            sortedArticles.add(articleDTOs.get(i));
        }
        return sortedArticles;
    }

    private List<ArticleDTO> fetchArticleDTOs(List<Long> articleIds) {
        System.out.println("从Redis上去取文章数据");
        return new ArrayList<ArticleDTO>();
    }

    private List<String> fetchUserFeatures(Long userId) {
        System.out.println("从Redis上去取用户特征数据");
        return new ArrayList<String>();
    }




}
